<?php 

class Ladder{

	function getBestRankings($steamID, $count="*"){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start getBestRankings <br>\n";

		if($steamID == "" || $steamID == 0){
			$steamID = $_SESSION['user']['steamID'];
		}

		if($steamID > 0){
			$MatchMode = new MatchMode();
			$MatchType = new MatchType();

			$matchModes = $MatchMode->getAllMatchModes();
			$matchTypes = $MatchType->getAllMatchTypes();

			$data = array();
			if(is_array($matchTypes) && count($matchTypes) > 0){
				foreach($matchTypes as $k => $matchtype){

					if(is_array($matchModes) && count($matchModes) > 0){

						foreach($matchModes as $k => $matchmode){
							$tmpData = array();
							$sql = "SELECT SteamID, Elo
									FROM UserElo
									WHERE MatchModeID = ".(int) $matchmode['MatchModeID']." AND MatchTypeID = ".(int) $matchtype['MatchTypeID']."
											 AND (Wins > 0 OR Loses > 0)
											ORDER BY Elo DESC
											";
							$tmpData = $DB->multiSelect($sql);
							$ret['debug'] .= $sql;
							//$ret['debug'] .= p($tmpData,1);
							if(is_array($tmpData) && count($tmpData) > 0){
								$ret['debug'] .= "NOT NULL";
								$key = recursive_array_search($steamID, $tmpData);
								$ret['debug'] .= "KEY: ".$key;
								if($key >= 0){
									$ret['debug'] .= "ID:".$steamID." ".p($key,1);
									$tmpArray['MatchTypeID'] = $matchtype['MatchTypeID'];
									$tmpArray['MatchModeID'] =$matchmode['MatchModeID'];
									$tmpArray['MatchType'] = $matchtype['Name'];
									$tmpArray['MatchMode'] = $matchmode['Name'];
									$tmpArray['MatchModeShortcut'] = $matchmode['Shortcut'];
									$tmpArray['position'] = ($key+1);
									$tmpArray['Elo'] = $tmpData[$key]['Elo'];
									$data[] = $tmpArray;
								}
							}
						}
					}

				}
			}
			// Ergebnisse nach position aufsteigend sortieren
			$orderedPositions = orderArrayBy($data,'position',SORT_ASC);

			// je nachdem wieviele Ergebnisse zur�ckgegeben werden soll, die besten n zur�ckgeben
			if($count != "*"){
				$count = (int) $count;

				for($i=0; $i<$count; $i++){
					if($orderedPositions[$i]['position'] > 0){
						$ret['data'][] = $orderedPositions[$i];
					}
				}

			}
			$ret['debug'] .= p($ret['data'],1);

			$ret['status'] = true;
		}
		else{
			$ret['status'] = "SteamID = 0";
		}

		$ret['debug'] .= "End getBestRankings <br>\n";

		return $ret;
	}

	function getGeneralRanking($steamID, $elo){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		if($steamID > 0){
			$tmpData = array();
			$sql = "SELECT SteamID, Rank
					FROM UserStats
					WHERE Rank >= ".(int) $elo."
							AND (Wins > 0 OR Loses > 0)
							ORDER BY Rank DESC
							";
			$tmpData = $DB->multiSelect($sql);
				
			if(is_array($tmpData) && count($tmpData) > 0){
				$position=1;
				foreach($tmpData as $k => $v){
					if($v['SteamID'] == $steamID){
						$ret['position'] = $position;
					}
					else{
						$position++;
					}
				}
			}
				
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "SteamID 0";
		}
		return $ret;
	}
}

?>